package org.kilian;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.security.GeneralSecurityException;
import java.util.HashMap;

import org.GenusApis.GenusApis;
import org.GenusApis.lib.HyvesApiException;
import org.GenusApis.lib.OAuthAccessToken;
import org.GenusApis.lib.OAuthConsumer;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class HyvesFotolijstje extends Activity {
	
    public static final String CONSUMER_KEY = "";
    public static final String CONSUMER_SECRET = "";
    public static final String HA_VERSION = "2.0";

    public static final String PREF_TOKEN = "AccessToken";
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        
        OAuthAccessToken accessToken = getAccessTokenFromPref(this);
        if (accessToken != null) {
			//jeej api met accesstoken
	        OAuthConsumer oauthConsumer = new OAuthConsumer(HyvesFotolijstje.CONSUMER_KEY, HyvesFotolijstje.CONSUMER_SECRET);
			GenusApis genusApis = new GenusApis(oauthConsumer, HyvesFotolijstje.HA_VERSION);
			OAuthAccessToken accessToken = HyvesFotolijstje.getAccessTokenFromPref(this);
			HashMap<String, String> params = new HashMap<String, String>();
			params.put("userid", accessToken.getUserid());
			JSONObject result = genusApis.doMethod("albums.getByUser", params, accessToken);
			Log.i("APIRESULT", result.toString());
        } else {
        	initiateAuthorization();
        }
    }
    
    public final static OAuthAccessToken getAccessTokenFromPref(Context context) {
		SharedPreferences settings = context.getSharedPreferences(PREF_TOKEN, 0);
        if (settings.getString("oauth_token", null) != null) {
        	return new OAuthAccessToken(
        										settings.getString("oauth_token", null), 
        										settings.getString("oauth_token_secret", null), 
        										settings.getString("userid", null), 
        										settings.getString("methods", null), 
        										settings.getString("expiration", null));
        } else {
        	return null;
        }
    }

	private void initiateAuthorization() {
        try {
        	Toast toast = Toast.makeText(this, "Toestemming van Hyves nodig!", Toast.LENGTH_LONG);
        	toast.show();
        	
          	OAuthConsumer oauthConsumer = new OAuthConsumer(HyvesFotolijstje.CONSUMER_KEY, HyvesFotolijstje.CONSUMER_SECRET);
            GenusApis genusApis = new GenusApis(oauthConsumer, HyvesFotolijstje.HA_VERSION);
        	
            HashMap<String, String> params = new HashMap<String, String>();
            params.put("methods", "albums.getByUser,media.getByAlbum");
            params.put("expirationtype", "infinite");
			JSONObject result = genusApis.doMethod("auth.requesttoken", params);
		    String callbackUrl = "hyves-fotolijstje-android-app://www.hyves-fotolijstje.com/?s="+URLEncoder.encode(result.getString("oauth_token_secret"));
			Intent i = new Intent(Intent.ACTION_VIEW);
			i.setData(Uri.parse("http://www.hyves.nl/api/authorize/"+
			                                  "?oauth_token="+URLEncoder.encode(result.getString("oauth_token"))+
			                                  "&oauth_callback="+URLEncoder.encode(callbackUrl)));
			startActivity(i);
		} catch (GeneralSecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HyvesApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
}